package com.heartware.sh.blackhole.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.heartware.sh.blackhole.R;
import com.heartware.sh.blackhole.adapter.ListAdapter;
import com.heartware.sh.blackhole.entity.test.MyEntity;
import com.heartware.sh.blackhole.ui.fragment.base.BaseFragment;
import com.heartware.sh.blackhole.ui.fragment.base.BaseNullFragment;
import com.heartware.sh.blackhole.ui.fragment.view.LoadingPager;
import com.widget.recyclerview.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/25 0025.
 */
public class MyFragment extends BaseNullFragment {

    private String type = "temp";

    private TextView tv;


    private SwipeRefreshLayout srl;
    private RecyclerView rv;

    private ListAdapter adapter;

    private List<MyEntity> list = new ArrayList<MyEntity>();

    private boolean loading;

    @Override
    public void load() {

        setState(LoadingPager.LoadingResult.LOADING);

        if(adapter!= null){
            if(adapter.getItemCount() > 0){
                createData(true);
            }else{
                createData(false);
            }
        }else{
            createData(false);
        }

    }

    @Override
    public View createSuccessView() {
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_my, null);
    }

    //    @Override
//    public int getLayoutId() {
//        return R.layout.fragment_my;
//    }

    @Override
    public void init() {

        tv = (TextView) findViewById(R.id.tv);

        tv.setText(type);

        srl = (SwipeRefreshLayout) findViewById(R.id.srl);
        rv = (RecyclerView) findViewById(R.id.rv);

        /** 设置item动画*/
        rv.setItemAnimator(new DefaultItemAnimator());
        /** 设置item Divider  ListView*/
        rv.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
        /** 设置Recyclerview显示样式,   类似Listview*/
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext());

        rv.setLayoutManager(manager);

        adapter = new ListAdapter(list);

        rv.setAdapter(adapter);

        setListener();//--------
    }

    @Override
    public void setListener() {
        super.setListener();


        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //TODO 刷新监听...

                list.clear();

                adapter.notifyDataSetChanged();

//                createData(false);
                load();
            }
        });


        rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

                int totalItemCount = layoutManager.getItemCount();

                int lastVisibleItem = layoutManager.findLastVisibleItemPosition();

//                if (!loading && totalItemCount < (lastVisibleItem + Constant.VISIBLE_THRESHOLD)) {
                if (!loading) {
//                    new ArticleTask(mActivity).execute(adapter.getBottomArticleId());
//                    createData(true);
                    load();
                }
            }
        });


    }


    public void createData(final boolean bo){

        if(loading){
            return;
        }
        loading = true;

        setState(LoadingPager.LoadingResult.SUCCESS);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                MyEntity me = null;

                if(bo){
                    list.remove(null);
                }

                for (int i=0; i<20; i++){
                    me = new MyEntity();
                    me.setTitle(type+":  数据 "+(bo?"加载":"")+i);
                    list.add(me);
                }

                list.add(null);


                adapter.notifyDataSetChanged();
                loading = false;

                if(srl.isRefreshing()){
                    srl.setRefreshing(false);
                }

            }
        }, 2000);

    }


    public void setType(String type){
        this.type = type;
    }


}
